var current_Version = 2.20;
angular.module('starter.controllers', ['ionic', 'httpService'])
//主界面进入控制
  .controller('homeCtrl', ['$scope', '$state', "$ionicPopup", "localStorageService", "noticeService", "staffService", "ticketOrderService", function ($scope, $state, $ionicPopup, localStorageService, noticeService, staffService, ticketOrderService) {
    if (localStorageService.get("phone") == undefined || localStorageService.get("phone") == null || localStorageService.get("password") == null || localStorageService.get("staffId") == undefined) {
      $state.go('signin', {}, {reload: true});
    }
    else {
      $scope.homeData = {};
      $scope.homeData.haveTickes = false;
      $scope.notice = {};
      //系统消息判断
      if (localStorageService.get("noticeLastTime")) {
        var noticeLastTime = localStorageService.get("noticeLastTime");
        var diffMin = (new Date().getTime() - noticeLastTime) / (1000 * 60);
        //五分钟消息更新
        if (diffMin > 5) {
          noticeService.getByCount({start: 0, count: 1}).then(function (response) {
            $scope.notice = response[0];
            localStorageService.setObject("lastNotice", $scope.notice);
            localStorageService.set("noticeLastTime", new Date().getTime())
          });
          //  haveTickets();
        }
        else {
          $scope.notice = localStorageService.getObject("lastNotice");
          //    localStorageService.set("noticeLastTime",new Date().getTime())
        }
      }
      else {
        noticeService.getByCount({start: 0, count: 1}).then(function (response) {
          $scope.notice = response[0];
          localStorageService.setObject("lastNotice", $scope.notice);
          localStorageService.set("noticeLastTime", new Date().getTime())
        })
      }
      //权限核对消息判断
      if (localStorageService.get("authorityInfoLastTime")) {
        var staffInfoLastTime = localStorageService.get("authorityInfoLastTime");
        var diffHour = (new Date().getTime() - staffInfoLastTime) / (1000 * 60 * 60);
        $scope.homeData.authority = localStorageService.getObject("authority");

        //两个小时内的身份验证更新
        if (diffHour > 2) {
          var data = {};
          data.phone = localStorageService.get("phone");
          data.password = localStorageService.get("password");
          data.app = "";
          if (ionic.Platform.isAndroid()) {
            data.app = "android-" + current_Version;
          }
          else {
            data.app = "webapp-" + current_Version;
          }
          staffService.heartAuthority(data).then(function (response) {
            localStorageService.set("authorityInfoLastTime", new Date().getTime());
            localStorageService.setObject("authority", response.authority);

            if (response.minVersion != undefined && response.minVersion > current_Version) {

              $ionicPopup.alert({
                title: "版本过期提示",
                template: "版本已停止使用,请下载新版本!"
              }).then(function () {
                localStorageService.clear();
                $state.go("signin");
                ionic.Platform.exitApp();
              });
            }
            if (response.latestVersion != undefined && response.latestVersion > current_Version) {

              $ionicPopup.alert({
                title: "发现更新版本V" + latestVersion,
                template: "为获得更好体验,请在'设置'-'版本更新'中更新到最新版本或使用百度手机助手或浏览器扫一扫下载最新版本"
              }).then(function () {

              });
            }
          }, function (response) {
            if (response.status == 1003) {
              $ionicPopup.alert({
                title: "验证信息",
                template: "身份验证失败!"
              });
              localStorageService.clear();
              $state.go("signin");
            }
          });
        }
      }
      else {
        var data = {};
        data.phone = localStorageService.get("phone");
        data.password = localStorageService.get("password");
        data.app = "";
        if (ionic.Platform.isAndroid()) {
          data.app = "android-" + current_Version;
        }
        else {
          data.app = "webapp-" + current_Version;
        }
        staffService.heartAuthority(data).then(function (response) {
          localStorageService.set("authorityInfoLastTime", new Date().getTime());
          localStorageService.setObject("authority", response.authority);
          $scope.homeData.authority = localStorageService.getObject("authority");
          if (response.minVersion != undefined && response.minVersion > current_Version) {
            $ionicPopup.alert({
              title: "版本过期提示",
              template: "版本已停止使用,请下载新版本!"
            }).then(function () {
              localStorageService.clear();
              ionic.Platform.exitApp();
            });
          }
          if (response.latestVersion != undefined && response.latestVersion > current_Version) {

            $ionicPopup.alert({
              title: "发现更新版本V" + latestVersion,
              template: "为获得更好体验,请在'设置'-'版本更新'中更新到最新版本或使用百度手机助手或浏览器扫一扫下载最新版本"
            }).then(function () {

            });
          }
        }, function (response) {
          if (response.status = 1003) {
            $ionicPopup.alert({
              title: "验证信息",
              template: "身份验证失败!"
            });
            localStorageService.clear();
            $state.go("signin");
          }
        });
      }
      $scope.homeData.level = localStorageService.get("level");
      $scope.homeData.name = localStorageService.get("userName");
      var haveTickets = function () {//检测是否有未使用车票
        var staffId = localStorageService.get("staffId");
        ticketOrderService.getValidateTicketsInfoByStaffId({staffId: staffId}).then(function (response) {
          $scope.homeData.haveTickes = true;
        }, function (response) {
          $scope.homeData.haveTickes = false;
        }).finally(function () {
        });
      }
      //  haveTickets();
    }
  }])
  .controller("modifyStaffCtrl", ["$scope", "$state", "$ionicPopup", "localStorageService", "staffService", "departmentService", function ($scope, $state, $ionicPopup, localStorageService, staffService, departmentService) {
    $scope.staff = {};
    if (localStorageService.get("userName") && localStorageService.get("phone") && localStorageService.get("password")) {
      $scope.staff.name = localStorageService.get("userName");
      $scope.staff.phone = localStorageService.get("phone");
      if ($scope.staff.phone.length < 10) {
        $scope.staff.phone = "";
      }
      $scope.staff.password = localStorageService.get("password");
    }
    else {
      $ionicPopup.alert({
        title: "验证信息",
        template: "用户验证失败,请重新登录"
      });
      $state.go("signin");
    }
    var staffId = localStorageService.get("staffId");
    if (staffId == undefined) {
      $ionicPopup.alert({
        title: "消息",
        template: "请重新登录"
      })
      return;
    }
    staffService.getStaffInfoByStaffId({staffId: staffId}).then(function (response) {
      $scope.staff = response[0];
    });

    departmentService.getAll().then(function (response) {
      $scope.departments = response;
    });
    $scope.statuses = [{id: '0', name: "正式职工"}, {id: '1', name: "招聘"}, {id: '2', name: "劳务派遣"}, {
      id: '3',
      name: "退休返聘"
    }, {id: '4', name: "离退休"}];
    $scope.sexes = [{id: 1, sex: '男'}, {id: 0, sex: '女'}];

    $scope.submit = function () {
      $scope.staff.id = localStorageService.get("staffId");
      delete  $scope.staff.lastlogindate;
      delete  $scope.staff.lastloginip;
      delete  $scope.staff.iseabled;
      delete  $scope.staff.departmentname;
      staffService.update($scope.staff).then(function (response) {
        $ionicPopup.alert({
          title: "提示",
          template: "资料修改成功!请重新登录!",
          okText: "好的"
        }).then(function () {
          $state.go("signin");
        });
      }, function (response) {
        $ionicPopup.alert({
          title: "提示",
          template: response.message,
          okText: "好的"
        });
      })
    }

  }])
  .controller('AccountCtrl', ["$scope", '$state', "$ionicPopup", "staffService", "localStorageService", function ($scope, $state, $ionicPopup, staffService, localStorageService) {
    if (localStorageService.get("phone") == undefined || localStorageService.get("phone") == null || localStorageService.get("password") == null || localStorageService.get("staffId") == undefined) {
      $state.go('signin', {}, {reload: true});
    }
    var staffId = localStorageService.get("staffId");
    if (staffId == undefined) {
      $ionicPopup.alert({
        title: "消息",
        template: "请重新登录"
      })
      return;
    }
    $scope.logout = function () {
      localStorageService.clear();
      $state.go('signin', {});
    }
    staffService.getStaffInfoByStaffId({staffId: staffId}).then(function (response) {
      $scope.staff1 = response[0];
    }, function () {
      $ionicPopup.alert({
        title: "消息",
        template: "获取个人数据失败"
      })
    });
  }])
  .controller("holi0Ctrl", ["$scope", "$state", "$ionicPopup", "$interval", "busesService", "busOrderService", function ($scope, $state, $ionicPopup, $interval, busesService, busOrderService) {
    $scope.getNewBuses = function () {
      busOrderService.getNearestBusorder0().then(function (response) {
        $scope.busOrder = response;
      });
      busesService.getNearestBuses0().then(function (response) {
        $scope.buses = response;
      }, function (response) {
        $ionicPopup.alert({
          title: "提示",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>服务器未开放车次数据或网络问题,获取数据失败！</p>",
          okText: '确定'
        });
      }).finally(function () {
        $scope.$broadcast('scroll.refreshComplete');
      });
    };
    $scope.getNewBuses();

    $scope.busEnter = function (index) {
      $state.go("seat_detail", {bus: $scope.buses[index], orientation: 0}, {reload: true});
    };
  }])
  .controller("holi1Ctrl", ["$scope", "$state", "$ionicPopup", "$interval", "busesService", "busOrderService", function ($scope, $state, $ionicPopup, $interval, busesService, busOrderService) {
    $scope.getNewBuses = function () {

      busOrderService.getNearestBusorder1().then(function (response) {
        $scope.busOrder = response;
      });

      busesService.getNearestBuses1().then(function (response) {
        $scope.buses = response;
      }, function (response) {
        $ionicPopup.alert({
          title: "提示",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>服务器未开放车次数据或网络问题,获取数据失败！</p>",
          okText: '确定'
        });
      }).finally(function () {
        $scope.$broadcast('scroll.refreshComplete');
      });
    }
    $scope.getNewBuses();

    $scope.busEnter = function (index) {
      $state.go("seat_detail1", {bus: $scope.buses[index], orientation: 0}, {reload: true});
    };
  }])
  .controller("seatDetailCtrl", ["$scope", "$state", "$ionicPopup", "$interval", "$stateParams", "$ionicModal", "busesService", "busOrderService", "ticketOrderService", "localStorageService", "relativeService", function ($scope, $state, $ionicPopup, $interval, $stateParams, $ionicModal, busesService, busOrderService, ticketOrderService, localStorageService, relativeService) {
    $scope.bus = $stateParams.bus;
    $scope.ticketInfoes = [];
    $scope.seats = [];
    //初始化车位
    var initialSeats = function () {
      $scope.seats = [];
      var rowCounts = parseInt($scope.bus.seatcount / 4);
      for (var i = 0; i < rowCounts; i++) {
        var seat = {};
        seat.num1 = 4 * i + 1;
        seat.num2 = 4 * i + 2;
        seat.num3 = 4 * i + 3;
        seat.num4 = 4 * i + 4;

        seat.name1 = '';
        seat.name2 = '';
        seat.name3 = '';
        seat.name4 = '';

        seat.type1 = 0;
        seat.type2 = 0;
        seat.type3 = 0;
        seat.type4 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.type;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.type;
          }
          if (value.seatNumber == seat.num3) {
            seat.name3 = value.seatName;
            seat.type3 = value.type;
          }
          if (value.seatNumber == seat.num4) {
            seat.name4 = value.seatName;
            seat.type4 = value.type;
          }
        });
        $scope.seats.push(seat);
      }
      var yushu = $scope.bus.seatcount % 4;
      if (yushu == 3) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.num2 = 2 + rowCounts * 4;
        seat.num3 = 3 + rowCounts * 4;
        seat.name1 = '';
        seat.name2 = '';
        seat.name3 = '';
        seat.type1 = 0;
        seat.type2 = 0;
        seat.type3 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.type;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.type;
          }
          if (value.seatNumber == seat.num3) {
            seat.name3 = value.seatName;
            seat.type3 = value.type;
          }
        });
        $scope.seats.push(seat);
      }
      if (yushu == 2) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.num2 = 2 + rowCounts * 4;

        seat.name1 = '';
        seat.name2 = '';

        seat.type1 = 0;
        seat.type2 = 0;

        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.type;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.type;
          }

        });
        $scope.seats.push(seat);
      }
      if (yushu == 1) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.name1 = '';
        seat.type1 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.type;
          }
        });
        $scope.seats.push(seat);
      }
    }

    var relationInitial = function () {
      $scope.getRelatives();
      $scope.ticketData.relativeChecked = false;
      $scope.$watch("ticketData.relativeChecked", function (value) {
        if (value) {
          if ($scope.ticketData.selectedRelative != undefined)
            $scope.ticketData.seatname = $scope.ticketData.selectedRelative.name;
        }
        else {
          $scope.ticketData.seatname = localStorageService.get("userName");
        }
      });
    }
    //模态框
    $ionicModal.fromTemplateUrl('ticketSeatModal', {
      scope: $scope,
      animation: 'slide-in-up'
    }).then(function (modal) {
      $scope.modal = modal;
    });
    $scope.openModal = function () {
      $scope.modal.show();
    };
    $scope.closeModal = function () {
      $scope.modal.hide();
    };
    $scope.$on('$destroy', function () {
      $scope.modal.remove();
    });
    $scope.ticketData = {};
    //获取亲属
    $scope.getRelatives = function () {
      if (!localStorageService.get("staffId")) {
        $ionicPopup.alert({
          title: "验证",
          template: "用户验证出现问题,请重新登录"
        });
        $state.go("signin");
      }
      $scope.ticketData.relatives = {};
      relativeService.getByStaffId({staffId: localStorageService.get("staffId")}).then(function (response) {
        $scope.ticketData.relatives = response;
      });
    }
    //亲属变化
    $scope.ticketData.selectChange = function () {
      if ($scope.ticketData.selectedRelative != undefined) {
        $scope.ticketData.seatname = $scope.ticketData.selectedRelative.name;
      }
    }
    $scope.ticketSeat = function (num) {
      $scope.ticketData.num = num;
      relationInitial();
      $scope.openModal();//打开
    }
    $scope.ticketData.insert = function () {
      var ticketInfo = {};
      ticketInfo.phone = localStorageService.get("phone");
      ticketInfo.password = localStorageService.get("password");
      ticketInfo.seatNumber = $scope.ticketData.num;
      ticketInfo.seatName = $scope.ticketData.seatname;
      ticketInfo.busId = $scope.bus.id;
      ticketInfo.staffId = localStorageService.get("staffId");
      if (ticketInfo.seatName == undefined || ticketInfo.seatName == "") {
        $ionicPopup.alert({
          title: "提示",
          template: "乘坐人信息出错"
        });
        return;
      }
      ticketOrderService.insert(ticketInfo).then(function (response) {
        ticketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
          $scope.ticketInfoes = response;
          initialSeats();
        }, function (response) {
          initialSeats();
        });
        $scope.closeModal();
        $ionicPopup.alert({
          title: "提示",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #4fd2c2'>订票成功！</p>",
          okText: '确定'
        });
      }, function (response) {
        ticketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
          $scope.ticketInfoes = response;
          initialSeats();
        }, function (response) {
          initialSeats();
        });
        $ionicPopup.alert({
          title: "提示",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;'>" + response.message + "</p>",
          okText: '确定'
        });
      });
    };
    ticketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
      $scope.ticketInfoes = response;
      $scope.seats = [];
      initialSeats();
    }, function (response) {
      initialSeats();
    });
  }])
  // .controller("seatDetail1Ctrl", ["$scope", "$state", "$ionicPopup", "$interval", "$stateParams", "$ionicModal", "busesService", "busOrderService", "ticketOrderService", "localStorageService", function ($scope, $state, $ionicPopup, $interval, $stateParams, $ionicModal, busesService, busOrderService, ticketOrderService, localStorageService) {
  //   $scope.bus = $stateParams.bus;
  //   $scope.ticketInfoes = [];
  //   $scope.seats = [];
  //   var initialSeats = function () {
  //     $scope.seats = [];
  //     var rowCounts = parseInt($scope.bus.seatcount / 4);
  //     for (var i = 0; i < rowCounts; i++) {
  //       var seat = {};
  //       seat.num1 = 4 * i + 1;
  //       seat.num2 = 4 * i + 2;
  //       seat.num3 = 4 * i + 3;
  //       seat.num4 = 4 * i + 4;
  //
  //       seat.name1 = '';
  //       seat.name2 = '';
  //       seat.name3 = '';
  //       seat.name4 = '';
  //
  //       seat.type1 = 0;
  //       seat.type2 = 0;
  //       seat.type3 = 0;
  //       seat.type4 = 0;
  //       angular.forEach($scope.ticketInfoes, function (value) {
  //         if (value.seatNumber == seat.num1) {
  //           seat.name1 = value.seatName;
  //           seat.type1 = value.type;
  //         }
  //         if (value.seatNumber == seat.num2) {
  //           seat.name2 = value.seatName;
  //           seat.type2 = value.type;
  //         }
  //         if (value.seatNumber == seat.num3) {
  //           seat.name3 = value.seatName;
  //           seat.type3 = value.type;
  //         }
  //         if (value.seatNumber == seat.num4) {
  //           seat.name4 = value.seatName;
  //           seat.type4 = value.type;
  //         }
  //       });
  //       $scope.seats.push(seat);
  //     }
  //     var yushu = $scope.bus.seatcount % 4;
  //     if (yushu == 3) {
  //       var seat = {};
  //       seat.num1 = 1 + rowCounts * 4;
  //       seat.num2 = 2 + rowCounts * 4;
  //       seat.num3 = 3 + rowCounts * 4;
  //       seat.name1 = '';
  //       seat.name2 = '';
  //       seat.name3 = '';
  //       seat.type1 = 0;
  //       seat.type2 = 0;
  //       seat.type3 = 0;
  //       angular.forEach($scope.ticketInfoes, function (value) {
  //         if (value.seatNumber == seat.num1) {
  //           seat.name1 = value.seatName;
  //           seat.type1 = value.type;
  //         }
  //         if (value.seatNumber == seat.num2) {
  //           seat.name2 = value.seatName;
  //           seat.type2 = value.type;
  //         }
  //         if (value.seatNumber == seat.num3) {
  //           seat.name3 = value.seatName;
  //           seat.type3 = value.type;
  //         }
  //       });
  //       $scope.seats.push(seat);
  //     }
  //     if (yushu == 2) {
  //       var seat = {};
  //       seat.num1 = 1 + rowCounts * 4;
  //       seat.num2 = 2 + rowCounts * 4;
  //
  //       seat.name1 = '';
  //       seat.name2 = '';
  //
  //       seat.type1 = 0;
  //       seat.type2 = 0;
  //
  //       angular.forEach($scope.ticketInfoes, function (value) {
  //         if (value.seatNumber == seat.num1) {
  //           seat.name1 = value.seatName;
  //           seat.type1 = value.type;
  //         }
  //         if (value.seatNumber == seat.num2) {
  //           seat.name2 = value.seatName;
  //           seat.type2 = value.type;
  //         }
  //
  //       });
  //       $scope.seats.push(seat);
  //     }
  //     if (yushu == 1) {
  //       var seat = {};
  //       seat.num1 = 1 + rowCounts * 4;
  //       seat.name1 = '';
  //       seat.type1 = 0;
  //       angular.forEach($scope.ticketInfoes, function (value) {
  //         if (value.seatNumber == seat.num1) {
  //           seat.name1 = value.seatName;
  //           seat.type1 = value.type;
  //         }
  //       });
  //       $scope.seats.push(seat);
  //     }
  //
  //   }
  //   $ionicModal.fromTemplateUrl('ticketSeatModal', {
  //     scope: $scope,
  //     animation: 'slide-in-up'
  //   }).then(function (modal) {
  //     $scope.modal = modal;
  //   });
  //   $scope.openModal = function () {
  //     $scope.modal.show();
  //   };
  //   $scope.closeModal = function () {
  //     $scope.modal.hide();
  //   };
  //   $scope.ticketSeat = function (num) {
  //     $scope.ticketData = {};
  //     $scope.ticketData.num = num;
  //     $scope.ticketData.seatname = localStorageService.get("userName");
  //     var ticketInfo = {};
  //     ticketInfo.phone = localStorageService.get("phone");
  //     ticketInfo.password = localStorageService.get("password");
  //     ticketInfo.seatNumber = num;
  //     ticketInfo.seatName = $scope.ticketData.seatname;
  //     ticketInfo.busId = $scope.bus.id;
  //     ticketInfo.staffId = localStorageService.get("staffId");
  //     $scope.ticketData.insert = function () {
  //       ticketOrderService.insert(ticketInfo).then(function (response) {
  //         ticketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
  //           $scope.ticketInfoes = response;
  //           initialSeats();
  //         }, function (response) {
  //           initialSeats();
  //         });
  //         $scope.closeModal();
  //         $ionicPopup.alert({
  //           title: "提示",
  //           template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #4fd2c2'>订票成功！</p>",
  //           okText: '确定'
  //         });
  //       }, function (response) {
  //         $ionicPopup.alert({
  //           title: "提示",
  //           template: "<p style='font-size:16px;font-weight: bold;text-align: center;'>" + response.message + "</p>",
  //           okText: '确定'
  //         });
  //       });
  //     }
  //     $scope.openModal();
  //   }
  //   ticketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
  //     $scope.ticketInfoes = response;
  //     $scope.seats = [];
  //     initialSeats();
  //   }, function (response) {
  //     initialSeats();
  //   });
  //
  //
  // }])
  .controller("seatDetail0WorkCtrl", ["$scope", "$state", "$ionicPopup", "$interval", "$stateParams", "$ionicModal", "workBusesService", "workTicketOrderService", "localStorageService","relativeService", function ($scope, $state, $ionicPopup, $interval, $stateParams, $ionicModal, workBusesService, workTicketOrderService, localStorageService,relativeService) {
    $scope.bus = $stateParams.bus;
    $scope.ticketInfoes = [];
    $scope.seats = [];
    var initialSeats = function () {
      $scope.seats = [];
      var rowCounts = parseInt($scope.bus.seatsCount / 4);
      for (var i = 0; i < rowCounts; i++) {
        var seat = {};
        seat.num1 = 4 * i + 1;
        seat.num2 = 4 * i + 2;
        seat.num3 = 4 * i + 3;
        seat.num4 = 4 * i + 4;

        seat.name1 = '';
        seat.name2 = '';
        seat.name3 = '';
        seat.name4 = '';

        seat.type1 = 0;
        seat.type2 = 0;
        seat.type3 = 0;
        seat.type4 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.auditStatus;
          }
          if (value.seatNumber == seat.num3) {
            seat.name3 = value.seatName;
            seat.type3 = value.auditStatus;
          }
          if (value.seatNumber == seat.num4) {
            seat.name4 = value.seatName;
            seat.type4 = value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }
      var yushu = $scope.bus.seatsCount % 4;
      if (yushu == 3) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.num2 = 2 + rowCounts * 4;
        seat.num3 = 3 + rowCounts * 4;
        seat.name1 = '';
        seat.name2 = '';
        seat.name3 = '';
        seat.type1 = 0;
        seat.type2 = 0;
        seat.type3 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.auditStatus;
          }
          if (value.seatNumber == seat.num3) {
            seat.name3 = value.seatName;
            seat.type3 = value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }
      if (yushu == 2) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.num2 = 2 + rowCounts * 4;

        seat.name1 = '';
        seat.name2 = '';

        seat.type1 = 0;
        seat.type2 = 0;

        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.auditStatus;
          }

        });
        $scope.seats.push(seat);
      }
      if (yushu == 1) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.name1 = '';
        seat.type1 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }

    }
    $ionicModal.fromTemplateUrl('ticketSeatModal', {
      scope: $scope,
      animation: 'slide-in-up'
    }).then(function (modal) {
      $scope.modal = modal;
    });
    $scope.openModal = function () {
      $scope.modal.show();
    };
    $scope.closeModal = function () {
      $scope.modal.hide();
    };
    $scope.ticketSeat = function (num) {
      $scope.ticketData = {};
      $scope.ticketData.num = num;
      $scope.ticketData.seatname = localStorageService.get("userName");
      var ticketInfo = {};
      ticketInfo.phone = localStorageService.get("phone");
      ticketInfo.password = localStorageService.get("password");
      ticketInfo.seatNumber = num;
      ticketInfo.seatName = $scope.ticketData.seatname;
      ticketInfo.busId = $scope.bus.id;

      ticketInfo.staffId = localStorageService.get("staffId");
      $scope.ticketData.insert = function () {
        ticketInfo.seatContent = $scope.ticketData.seatReasonDetail;
        ticketInfo.reasonId = $scope.ticketData.selectedSeatReason.id;
        workTicketOrderService.insert(ticketInfo).then(function (response) {
          workTicketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
            $scope.ticketInfoes = response;
            initialSeats();
          }, function (response) {
            initialSeats();
          });
          $scope.closeModal();
          $ionicPopup.alert({
            title: "提示",
            template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #4fd2c2'>"+ response.message +"</p>",
            okText: '确定'
          });
        }, function (response) {
          $ionicPopup.alert({
            title: "提示",
            template: "<p style='font-size:16px;font-weight: bold;text-align: center;'>" + response.message + "</p>",
            okText: '确定'
          });
        });
      }
      $scope.openModal();
      workTicketOrderService.getWorkSeatReason().then(function (result) {
        $scope.seatReasons = result;
      });
    }
    workTicketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
      $scope.ticketInfoes = response;
      $scope.seats = [];
      initialSeats();
    }, function (response) {
      initialSeats();
    });
  }])
  .controller("seatDetail1WorkCtrl", ["$scope", "$state", "$ionicPopup", "$interval", "$stateParams", "$ionicModal", "workBusesService", "workTicketOrderService", "localStorageService","relativeService", function ($scope, $state, $ionicPopup, $interval, $stateParams, $ionicModal, workBusesService, workTicketOrderService, localStorageService,relativeService) {
    $scope.bus = $stateParams.bus;
    var modelselect="ticketSeatModal";
    if(!$scope.bus.isNeedAudit){
      modelselect="ticketSeatModalNoAudit";
    }
    $scope.ticketInfoes = [];
    $scope.seats = [];
    var initialSeats = function () {
      $scope.seats = [];
      var rowCounts = parseInt($scope.bus.seatsCount / 4);
      for (var i = 0; i < rowCounts; i++) {
        var seat = {};
        seat.num1 = 4 * i + 1;
        seat.num2 = 4 * i + 2;
        seat.num3 = 4 * i + 3;
        seat.num4 = 4 * i + 4;

        seat.name1 = '';
        seat.name2 = '';
        seat.name3 = '';
        seat.name4 = '';

        seat.type1 = 0;
        seat.type2 = 0;
        seat.type3 = 0;
        seat.type4 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.auditStatus;
          }
          if (value.seatNumber == seat.num3) {
            seat.name3 = value.seatName;
            seat.type3 = value.auditStatus;
          }
          if (value.seatNumber == seat.num4) {
            seat.name4 = value.seatName;
            seat.type4 = value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }
      var yushu = $scope.bus.seatsCount % 4;
      if (yushu == 3) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.num2 = 2 + rowCounts * 4;
        seat.num3 = 3 + rowCounts * 4;
        seat.name1 = '';
        seat.name2 = '';
        seat.name3 = '';
        seat.type1 = 0;
        seat.type2 = 0;
        seat.type3 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.auditStatus;
          }
          if (value.seatNumber == seat.num3) {
            seat.name3 = value.seatName;
            seat.type3 = value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }
      if (yushu == 2) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.num2 = 2 + rowCounts * 4;

        seat.name1 = '';
        seat.name2 = '';

        seat.type1 = 0;
        seat.type2 = 0;

        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.auditStatus;
          }

        });
        $scope.seats.push(seat);
      }
      if (yushu == 1) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.name1 = '';
        seat.type1 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }

    }
    var relationInitial = function () {
      $scope.getRelatives();
      $scope.ticketData.relativeChecked = false;
      $scope.$watch("ticketData.relativeChecked", function (value) {
        if (value) {
          if ($scope.ticketData.selectedRelative != undefined)
            $scope.ticketData.seatname = $scope.ticketData.selectedRelative.name;
        }
        else {
          $scope.ticketData.seatname = localStorageService.get("userName");
        }
      });
    }
    $ionicModal.fromTemplateUrl(modelselect, {
      scope: $scope,
      animation: 'slide-in-up'
    }).then(function (modal) {
      $scope.modal = modal;
    });
    $scope.openModal = function () {
      $scope.modal.show();
    };
    $scope.closeModal = function () {
      $scope.modal.hide();
    };
    $scope.ticketData = {};
    //获取亲属
    $scope.getRelatives = function () {
      if (!localStorageService.get("staffId")) {
        $ionicPopup.alert({
          title: "验证",
          template: "用户验证出现问题,请重新登录"
        });
        $state.go("signin");
      }
      $scope.ticketData.relatives = {};
      relativeService.getByStaffId({staffId: localStorageService.get("staffId")}).then(function (response) {
        $scope.ticketData.relatives = response;
      });
    }


    $scope.ticketSeat = function (num) {
      $scope.ticketData = {};
      $scope.ticketData.num = num;
      //亲属变化
      $scope.ticketData.selectChange = function () {
        if ($scope.ticketData.selectedRelative != undefined) {
          $scope.ticketData.seatname = $scope.ticketData.selectedRelative.name;
        }
      }
      relationInitial();
      $scope.ticketData.seatname = localStorageService.get("userName");

      $scope.ticketData.insert = function () {
        var ticketInfo = {};
        ticketInfo.phone = localStorageService.get("phone");
        ticketInfo.password = localStorageService.get("password");
        ticketInfo.seatNumber = num;
        ticketInfo.seatName = $scope.ticketData.seatname;
        ticketInfo.busId = $scope.bus.id;
        ticketInfo.staffId = localStorageService.get("staffId");
        ticketInfo.seatContent = $scope.ticketData.seatReasonDetail||"test";
        if(!$scope.bus.isNeedAudit){
          ticketInfo.reasonId =2;
        }
        else {
          ticketInfo.reasonId = $scope.ticketData.selectedSeatReason.id
        }
        workTicketOrderService.insert(ticketInfo).then(function (response) {
          workTicketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
            $scope.ticketInfoes = response;
            initialSeats();
          }, function (response) {
            initialSeats();
          });
          $scope.closeModal();
          $ionicPopup.alert({
            title: "提示",
            template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #4fd2c2'>"+ response.message +"</p>",
            okText: '确定'
          });
        }, function (response) {
          $ionicPopup.alert({
            title: "提示",
            template: "<p style='font-size:16px;font-weight: bold;text-align: center;'>" + response.message + "</p>",
            okText: '确定'
          });
        });
      }
     $scope.openModal();
      workTicketOrderService.getWorkSeatReason().then(function (result) {
        $scope.seatReasons = result;
      });
    }
    workTicketOrderService.getInfoByBusId({busId: $scope.bus.id}).then(function (response) {
      $scope.ticketInfoes = response;
      $scope.seats = [];
      initialSeats();
    }, function (response) {
      initialSeats();
    });
  }])
  .controller("work0Ctrl", ["$scope", "$state", "$ionicPopup", "$interval", "workBusesService", function ($scope, $state, $ionicPopup, $interval, workBusesService) {
    $scope.buses = [];
    $scope.getNewBuses = function () {
      workBusesService.getValidate0().then(function (response) {
        $scope.buses = response;
      }, function (response) {
        $ionicPopup.alert({
          title: "提示",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>服务器未开放车次数据或网络问题,获取数据失败！</p>",
          okText: '确定'
        });
      }).finally(function () {
        $scope.$broadcast('scroll.refreshComplete');
      });
    }
    $scope.getNewBuses();
    $scope.busEnter = function (index) {
      $state.go("seats_detail_work0", {bus: $scope.buses[index]}, {reload: true});
    };
  }])
  .controller("work1Ctrl", ["$scope", "$state", "$ionicPopup", "$interval", "workBusesService", function ($scope, $state, $ionicPopup, $interval, workBusesService) {
    $scope.getNewBuses = function () {
      workBusesService.getValidate1().then(function (response) {
        $scope.buses = response;
      }, function (response) {
        $ionicPopup.alert({
          title: "提示",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>服务器未开放车次数据或网络问题,获取数据失败！</p>",
          okText: '确定'
        });
      }).finally(function () {
        $scope.$broadcast('scroll.refreshComplete');
      });
    }

    $scope.getNewBuses();
    $scope.busEnter = function (index) {
      $state.go("seats_detail_work1", {bus: $scope.buses[index]}, {reload: true});
    };
  }])
  .controller('myTicketsCtrlWork', function ($scope, $ionicPopup, localStorageService, workTicketOrderService, $timeout) {
    var staffId = localStorageService.get("staffId");
    $scope.noValidTickets = false;
    $scope.noInvalidTickets = false;
    $scope.getValidateTickets = function () {
      workTicketOrderService.getValidateTicketsInfoByStaffId({staffId: staffId}).then(function (response) {
        $scope.validateTickets = response;
      }, function (response) {
        $scope.validateTickets = response.body;
        $scope.noValidTickets = true;
      }).finally(function () {
      });
    };
    $scope.getValidateTickets();

    $scope.getInvalidateTicketsInfoByStaffId = function () {
      workTicketOrderService.getInvalidateTicketsInfoByStaffId({staffId: staffId}).then(function (response) {
        $scope.invalidateTickets = response;
      }, function (response) {
        $scope.invalidateTickets = response.body;
        $scope.noInvalidTickets = true;
      }).finally(function () {
      });
    };

    $scope.deleteTicket = function (ticket) {
      // 退票原因
      $scope.cancelReason = {};

      var deleteConfirm = $ionicPopup.confirm({
        title: "是否确定取消订票",
        subTitle: "请填写退票原因",
        scope: $scope,
        template: "<textarea style='width:100%;height:80px;' ng-model='cancelReason.content' placeholder='在此处填写原因供审核,不能为空'></textarea>",
        okText: '退票',
        cancelText: '取消'
      });
      deleteConfirm.then(function (res) {
          var ticketInfo = {};
          ticketInfo.phone = localStorageService.get("phone");
          ticketInfo.password = localStorageService.get("password");
          ticketInfo.seatNumber = ticket.seatNumber;
          ticketInfo.seatName = ticket.seatName;
          ticketInfo.ticketId = ticket.id;
          ticketInfo.busId = ticket.workBusesId;
          ticketInfo.staffId = localStorageService.get("staffId");
          ticketInfo.cancelReason = $scope.cancelReason.content;
          if (ticketInfo.cancelReason == "" || ticketInfo.cancelReason == null) {
            $ionicPopup.alert({
              title: "提示",
              template: "退票原因不能为空!"
            });
            return;
          }
          if (res) {
            workTicketOrderService.delete(ticketInfo).then(function (response) {
              $ionicPopup.alert({
                  title: "结果",
                  template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #4fd2c2'>已经成功取消订票！</p>",
                  okText: '确定'
                }
              );
              $scope.getValidateTickets();
            }, function (response) {
              $ionicPopup.alert({
                title: "结果",
                template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #4fd2c2'>" + response.message + "</p>",
                okText: '确定'
              })
            });
          }
        }
      );

    };

  })
  .controller("checkTicketCtrl", ["$scope", "$state", "$ionicPopup", "$interval", "$stateParams", "$ionicModal", "workBusesService", "workTicketOrderService", "localStorageService", function ($scope, $state, $ionicPopup, $interval, $stateParams, $ionicModal, workBusesService, workTicketOrderService, localStorageService) {
    $scope.data = {};
    $scope.data1 = {};
    //  $scope.staff.name = localStorageService.get("userName");
    $scope.data.phone = localStorageService.get("phone");
    $scope.data.staffId = localStorageService.get("staffId");
    $scope.data.password = localStorageService.get("password");
    $scope.noValidTickets = false;
    $scope.getAuditingTicketInfo = function () {
      workTicketOrderService.getAuditingTicketInfoByAuditor($scope.data).then(function (response) {
        $scope.noValidTickets = false;
        $scope.data1.validateTickets = response;
      }, function () {
        $scope.noValidTickets = true;
      });
    }
    $scope.getAuditingTicketInfo();

    $scope.getAuditedTicketInfo = function () {
      workTicketOrderService.getAuditedTicketInfoByAuditor($scope.data).then(function (response) {
        //    $scope.noValidTickets=false;
        $scope.data1.inValidateTickets = response;
      }, function () {
        //     $scope.noValidTickets=true;
      });
    }
    $scope.getAuditedTicketInfo();

    $scope.data1.detailInter = function (data) {
      $state.go("check/check_ticket_detail", {ticketInfo: data})
    };
  }])
  .controller("checkTicketDetailCtrl", ["$scope", "$state", "$stateParams", "$ionicPopup", "$interval", "$stateParams", "$ionicModal", "workBusesService", "workTicketOrderService", "localStorageService", function ($scope, $state, $stateParams, $ionicPopup, $interval, $stateParams, $ionicModal, workBusesService, workTicketOrderService, localStorageService) {
    $scope.ticketInfo = $stateParams.ticketInfo;
    $scope.data = {};
    $scope.data.phone = localStorageService.get("phone");
    $scope.data.staffId = localStorageService.get("staffId");
    $scope.data.password = localStorageService.get("password");
    $scope.ticketAgree = function (data) {
      $scope.data.workTicketId=data.ticketId;
      workTicketOrderService.auditTicketInfoByAuditor($scope.data).then(function(response){
        $ionicPopup.alert({
          title: "结果",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>已成功通过审核！</p>",
          okText: '好'
        });
        $state.go("check_ticket");
      },function(response){
        $ionicPopup.alert({
          title: "结果",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>"+response.message+"</p>",
          okText: '好'
        });
      });
    };
    $scope.ticketReject = function (data) {
      $scope.data.workTicketId=data.ticketId;
      $scope.data.auditreason="delete%%%Directly000233331111";
      workTicketOrderService.auditTicketInfoByAuditor($scope.data).then(function(response){
        $ionicPopup.alert({
          title: "结果",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>已拒绝该申请！</p>",
          okText: '好'
        });
        $state.go("check_ticket");
      },function(response){
        $ionicPopup.alert({
          title: "结果",
          template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>"+response.message+"</p>",
          okText: '好'
        });
      });

    };
  }])
  .controller('myTicketsCtrlHoli', function ($scope, $ionicPopup, localStorageService, ticketOrderService) {
    var staffId = localStorageService.get("staffId");
    $scope.noValidTickets = false;
    $scope.noInvalidTickets = false;
    $scope.getValidateTickets = function () {
      ticketOrderService.getValidateTicketsInfoByStaffId({staffId: staffId}).then(function (response) {
        $scope.validateTickets = response;
      }, function (response) {
        $scope.validateTickets = response.body;
        $scope.noValidTickets = true;
      }).finally(function () {
      });
    };
    $scope.getValidateTickets();

    $scope.getInvalidateTicketsInfoByStaffId = function () {
      ticketOrderService.getInvalidateTicketsInfoByStaffId({staffId: staffId}).then(function (response) {
        $scope.invalidateTickets = response;
      }, function (response) {
        $scope.invalidateTickets = response.body;
        $scope.noInvalidTickets = true;
      }).finally(function () {
      });
    };

    // 字数限制退票原因代码，先注释
    // $scope.deleteTicket= function (ticket) {
    //   // 退票原因
    //   $scope.cancelReason = {
    //     reasonLength: false,
    //   };
    //
    //   var deleteConfirm=$ionicPopup.show({
    //     title:"取消订票原因",
    //     scope: $scope,
    //     template:"<textarea style='width:100%;height:100px;' ng-model='cancelReason.content' placeholder='请至少输入10个字'></textarea><p style='height:14px;'><span ng-show='cancelReason.reasonLength' style='height:14px;font-size:14px;color:red;'>请至少输入10个字</span></p>",
    //     buttons: [
    //       { text: '取消' },
    //       {
    //         text: '确定',
    //         type: 'button-positive',
    //         onTap: function(e) {
    //           if ($scope.cancelReason.content.length < 10) {
    //             e.preventDefault(); // 阻止对话框关闭
    //
    //             $scope.cancelReason.reasonLength = true;
    //             $timeout(function() {
    //               $scope.cancelReason.reasonLength = false; // 3秒后关闭提示
    //             }, 3000);
    //           }
    //           return $scope.cancelReason.content;
    //         }
    //       }]
    //   });

    $scope.deleteTicket = function (ticket) {
      // 退票原因
      $scope.cancelReason = {};

      var deleteConfirm = $ionicPopup.confirm({
        title: "是否确定取消订票",
        subTitle: "请填写退票原因",
        scope: $scope,
        template: "<textarea style='width:100%;height:80px;' ng-model='cancelReason.content' placeholder='在此处填写退票原因供审核。注意：退票后2个小时内不可再次订票!'></textarea>",
        okText: '退票',
        cancelText: '取消'
      });
      deleteConfirm.then(function (res) {
          var ticketInfo = {};
          ticketInfo.phone = localStorageService.get("phone");
          ticketInfo.password = localStorageService.get("password");
          ticketInfo.seatNumber = ticket.seatNumber;
          ticketInfo.seatName = ticket.seatName;
          ticketInfo.ticketId = ticket.id;
          ticketInfo.busId = ticket.busesId;
          ticketInfo.staffId = localStorageService.get("staffId");
          ticketInfo.cancelReason = $scope.cancelReason.content;
          if (res) {
            ticketOrderService.delete(ticketInfo).then(function (response) {
              $ionicPopup.alert({
                  title: "结果",
                  template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #4fd2c2'>已经成功取消订票！</p>",
                  okText: '确定'
                }
              );
              $scope.getValidateTickets();
            }, function (response) {
              $ionicPopup.alert({
                title: "结果",
                template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #4fd2c2'>" + response.message + "</p>",
                okText: '确定'
              })
            });
          }
        }
      );

    };
  })
  .controller("addRelativeCtrl", ["$scope", "$state", "$ionicPopup", "localStorageService", "staffService", "relativeService", function ($scope, $state, $ionicPopup, localStorageService, staffService, relativeService) {
    if (!localStorageService.get("staffId")) {
      $ionicPopup.alert({
        title: "验证",
        template: "用户验证出现问题,请重新登录"
      });
      $state.go("signin");
    }
    $scope.relative = {};
    $scope.relations = [];
    $scope.relations.push({name: "子女"});
    $scope.relations.push({name: "配偶"});
    $scope.relations.push({name: "父母"});
    $scope.relative.name = "";
    //   $scope.selectedRelation = $scope.relations[0];
    $scope.relative.contact = "";
    $scope.relative.selected = $scope.relations[0];
    $scope.insertRelative = function () {
      $scope.relative.relation = $scope.relative.selected.name;
      if ($scope.relative.name == "" || $scope.relative.relation == "") {
        $ionicPopup.alert({
          title: "提示",
          template: "输入信息不完整",
          okText: "好"
        });
        return;
      }
      var data = {};
      data.staffid = localStorageService.get("staffId");
      data.relative = $scope.relative.relation;
      data.name = $scope.relative.name;
      data.contact = $scope.relative.contact;
      relativeService.insert(data).then(function (response) {
        $ionicPopup.alert({
          title: "结果",
          template: "成功!",
          okText: "好"
        });
        $state.go("my_family");
      }, function (response) {
        $ionicPopup.alert({
          title: "结果",
          template: response.message,
          okText: "确定"
        });
      });
    };
  }])
  .controller('myFamilyCtrl', ["$scope", "$state", "$ionicPopup", "localStorageService", "staffService", "relativeService", function ($scope, $state, $ionicPopup, localStorageService, staffService, relativeService) {
    if (!localStorageService.get("staffId")) {
      $ionicPopup.alert({
        title: "验证",
        template: "用户验证出现问题,请重新登录"
      });
      $state.go("signin");
    }
    $scope.getRelatives = function () {
      $scope.relatives = {};
      relativeService.getByStaffId({staffId: localStorageService.get("staffId")}).then(function (response) {
        $scope.relatives = response;
      });
    }
    $scope.getRelatives();
    $scope.deleteRelative = function (data) {
      var Modal = $ionicPopup.confirm({
        title: "删除亲属确认",
        template: "<p style='font-size:16px;font-weight: bold;text-align: center;color: #ff3366'>确定要删除亲属吗</p>",
        okText: "确定",
        cancelText: "取消"
      });
      Modal.then(function (res) {
        if (res) {
          relativeService.delete({id: data, staffId: localStorageService.get("staffId")}).then(function () {

            $ionicPopup.alert({
              title: "结果",
              template: "删除成功",
              okText: "好"
            });
            $scope.getRelatives();
          }, function (response) {
            $ionicPopup.alert({
              title: "结果",
              template: response.message,
              okText: "好"
            });
          });
        }

      })

    };

  }])
  .controller("noticeCtrl", ["$scope", "$state", "$ionicPopup", "localStorageService", "noticeService", function ($scope, $state, $ionicPopup, localStorageService, noticeService) {
    if (localStorageService.getObject("notices") != undefined && localStorageService.getObject("notices") != null) {
      $scope.notices = localStorageService.getObject("notices");
    }
    $scope.doRefresh = function () {
      noticeService.getAll().then(function (response) {
        if ($scope.notices[0] != undefined && response[0].id == $scope.notices[0].id) {
          return;
        }
        $scope.notices = response;
        localStorageService.setObject("notices", $scope.notices);

      }, function (response) {

      }).finally(function () {
        $scope.$broadcast('scroll.refreshComplete');
      });

    };
    $scope.doRefresh();
    $scope.backToHome = function () {
      $state.go("tab.home");
    }
    $scope.noticeDetail = function (index) {
      $state.go("notice_detail", {notice: $scope.notices[index]});
    }
  }])
  .controller("noticeDetailCtrl", ["$scope", "$ionicPopup", "$stateParams", function ($scope, $ionicPopup, $stateParams) {
    $scope.notice = $stateParams.notice;

  }])
  .controller("queryPhoneCtrl", ["$scope", "staffService", function ($scope, staffService) {
    $scope.keywords = {};
    $scope.search = function () {
      staffService.getByKeywords({keywords: $scope.keywords.word}).then(function (response) {
        $scope.staffInfoes = response;
      });
    }
  }])
  .controller("signupCtrl", ["$scope", '$state', "$ionicPopup", "staffService", "departmentService", "localStorageService", function ($scope, $state, $ionicPopup, staffService, departmentService, localStorageService) {
    $scope.staff = {};
    departmentService.getAll().then(function (response) {
      $scope.departments = response;
      $scope.staff.sex = $scope.sexes[0];
      $scope.staff.departmentid = response[0];
    });
    $scope.statuses = [{id: '0', name: "在职"}, {id: '1', name: "退休"}];
    $scope.sexes = [{id: 1, sex: '男'}, {id: 0, sex: '女'}];
    $scope.info = {};
    $scope.submit = function () {
      if ($scope.staff.password != $scope.info.password) {
        $ionicPopup.alert({
          title: "注册消息",
          template: "两次密码输入不一致"
        })
        return;
      }
      staffService.insert($scope.staff).then(function (response) {
        $ionicPopup.alert({
          title: "注册消息",
          template: "注册成功!!"
        })
        localStorageService.set("phone", $scope.staff.phone);
        localStorageService.set("password", $scope.staff.password);
        $state.go('signin', {});
      }, function (response) {
        $ionicPopup.alert({
          title: "注册消息",
          template: "注册失败"
        })
      })
    };
  }])
  .controller("signinCtrl", ["$scope", '$state', "$ionicPopup", "staffService", "localStorageService", function ($scope, $state, $ionicPopup, staffService, localStorageService) {
    $scope.staff = {};
    $scope.staff.password = '';
    $scope.staff.phone = '';
    if (localStorageService.get("phone") != undefined && localStorageService.get("password") != undefined) {
      $scope.staff.password = localStorageService.get("password");
      $scope.staff.phone = localStorageService.get("phone");
    }
    $scope.toSignUP = function () {
      $state.go("signup", {});
    }
    $scope.submit = function () {
      if ($scope.staff.password == '' || $scope.staff.phone == '') {
        $ionicPopup.alert({
          title: "登录消息",
          template: "请输入帐号和密码!!"
        });
        return;
      }
      staffService.validate({phone: $scope.staff.phone, password: $scope.staff.password}).then(function (response) {
        if (response.validateStatus == "error") {
          $ionicPopup.alert({
            title: "登录消息",
            template: "验证失败,请检查您的账户与密码!!"
          })
          return;
        }
        localStorageService.set("userName", response.name);
        localStorageService.set("staffId", response.id);
        if (response.validateStatus == "right") {
          localStorageService.set("phone", $scope.staff.phone);
          localStorageService.set("password", $scope.staff.password);
          $state.go("tab.home", {}, {reload: true});
        }
        else if (response.validateStatus == "message") {
          $ionicPopup.alert({
            title: "登录消息",
            template: response.message
          })
          return;
        }
        else if (response.validateStatus == "wrong") {
          $ionicPopup.alert({
            title: "登录消息",
            template: "账户已被冻结!!"
          })
          return;
        }

      })
    }

  }])
  .controller("settingCtrl", ["$scope", "$ionicPlatform", '$state', "$ionicPopup", "staffService", "localStorageService", "settingService", function ($scope, $state, $ionicPlatform, $ionicPopup, staffService, localStorageService, settingService) {
    if (localStorageService.get("phone") == undefined || localStorageService.get("phone") == null || localStorageService.get("password") == null || localStorageService.get("staffId") == undefined) {
      $state.go('signin', {}, {reload: true});
    }
    $scope.openInExternalBrowser = function (url) {
      window.open(url, '_system', 'location=yes');
    };
    $scope.exitApp = function () {
      ionic.Platform.exitApp();
    };
    $scope.setData = {};
    $scope.setData.isAndroid = ionic.Platform.isAndroid();
    $scope.updateSelf = function () {
      settingService.versionUrl().then(function (response) {
        if (response.version > current_Version) {
          $ionicPopup.show({
            title: '检测结果',
            template: "发现新版本！ V" + response.version + "；当前版本 V" + current_Version + "，请及时更新",
            subTitle: '',
            scope: $scope,
            buttons: [{
              text: '忽略',
              type: 'button-default',
              onTap: function () {
              }
            },
              {
                text: '更新',
                type: 'button-positive',
                onTap: function () {
                  return $scope.openInExternalBrowser(response.url);
                }
              }]
          });
        }
        else {
          $ionicPopup.alert({
            title: "检测结果",
            template: "已经是最新版本！ V" + current_Version
          });
        }
      });
    }
  }])
  .controller("settingUpdatePasswordCtrl", ["$scope", "$state", "$ionicPopup", "staffService", "localStorageService", function ($scope, $state, $ionicPopup, staffService, localStorageService) {
    $scope.password = {};
    $scope.updatePassword = function () {
      var data = {};
      if ($scope.password.new != $scope.password.confirm) {
        $ionicPopup.alert({
          title: "密码更改提示",
          template: "两次密码输入不一致!"
        });
        return;
      }
      data.phone = localStorageService.get("phone");
      data.password = $scope.password.old;
      data.newPassword = $scope.password.new;
      staffService.updatePassword(data).then(function (reponse) {
        localStorageService.clear();
        $ionicPopup.alert({
          title: "结果",
          template: "密码更改成功!"
        });
        $state.go("signin");
      }, function (response) {
        $ionicPopup.alert({
          title: "结果",
          template: response.message
        });
      });
    }
  }])
  .controller("settingAboutCtrl", ["$scope", "$sce",'$state', "$ionicPopup", "staffService", "localStorageService", "settingService", function ($scope,$sce, $state, $ionicPopup, staffService, localStorageService, settingService) {
    settingService.about().then(function (response) {
      $scope.setting = {};
      $scope.setting.about =$sce.trustAsHtml(response);
    });
  }])
  .controller("settingHelpCtrl", ["$scope", "$sce",'$state', "$ionicPopup", "staffService", "localStorageService", "settingService", function ($scope,$sce, $state, $ionicPopup, staffService, localStorageService, settingService) {
    settingService.help().then(function (response) {
      $scope.setting = {};
      $scope.setting.content = $sce.trustAsHtml(response);
    });
  }])
  .controller("settingBusRuleCtrl", ["$scope","$sce", '$state', "$ionicPopup", "staffService", "localStorageService", "settingService", function ($scope, $sce,$state, $ionicPopup, staffService, localStorageService, settingService) {
    settingService.busRule().then(function (response) {
      $scope.setting = {};
      $scope.setting.content =$sce.trustAsHtml(response);
    });
  }])
  .controller("settingSoftRuleCtrl", ["$scope", "$sce",'$state', "$ionicPopup", "staffService", "localStorageService", "settingService", function ($scope,$sce, $state, $ionicPopup, staffService, localStorageService, settingService) {
    settingService.softRule().then(function (response) {
      $scope.setting = {};
      $scope.setting.content = $sce.trustAsHtml(response);
    });
  }])
  .controller("settingFeedbackCtrl", ["$scope", '$state', "$ionicPopup", "staffService", "localStorageService", "settingService", function ($scope, $state, $ionicPopup, staffService, localStorageService, settingService) {
    $scope.setting = {};
    $scope.submit = function () {
      if ($scope.setting.feedback == undefined || $scope.setting.feedback == "") {
        $ionicPopup.alert({
          title: "提示",
          template: "请输入信息"
        })
        return;
      }
      if (!localStorageService.get("staffId")) {
        $ionicPopup.alert({
          title: "验证",
          template: "用户验证出现问题,请重新登录"
        });
        $state.go("signin");
      }
      var data = {};
      data.staffId = parseInt(localStorageService.get("staffId"));
      data.message = $scope.setting.feedback;
      settingService.feedback(data).then(function (response) {
        $ionicPopup.alert({
          title: "结果",
          template: "提交成功,谢谢您的宝贵意见!"
        })
        $scope.setting.feedback = "";
      });
    }
  }])
  .controller("carDetailCtrl", ["$scope", '$state', '$stateParams', "$ionicPopup", "staffService", "localStorageService", "settingService", function ($scope, $state, $stateParams, $ionicPopup, staffService, localStorageService, settingService) {
    $scope.category = $stateParams.category;
  }])
  .controller("perCarCtrl", ["$scope", '$state', "$ionicPopup", "staffService", "localStorageService", "carPoolingService", function ($scope, $state, $ionicPopup, staffService, localStorageService, carPoolingService) {
    var getdata = function () {
      carPoolingService.getValidateCarPooling().then(function (response) {
        $scope.categories = response;
      });
    }
    getdata();
    $scope.data = {};
    $scope.data.staffid = localStorageService.get("staffId");
    $scope.data.delete = function (data) {
      carPoolingService.delete({id: data.id, staffId: $scope.data.staffid}).then(function (response) {
        getdata();
        $ionicPopup.alert({
          title: "结果",
          template: "删除成功!"
        })

      });
    };
    $scope.carPoolingDetail = function (data) {
      $state.go("car_detail", {category: data})
    }
  }])
  .controller("carPoolingInsertCtrl", ["$scope", "$ionicPopup", "localStorageService", "carPoolingService", function ($scope, $ionicPopup, localStorageService, carPoolingService) {
    carPoolingService.getCategory().then(function (response) {
      $scope.categories = response;
    });
    $scope.category = {};


    $scope.submit = function () {
      var data = {};
      data.staffid = localStorageService.get("staffId");
      data.category = $scope.category.selectedCategory.id;
      data.content = $scope.category.content;
      var date = new Date($scope.category.date);
      data.expiredate = date.getTime();
      carPoolingService.insert(data).then(function (response) {
        $ionicPopup.alert({
          title: "结果",
          template: "成功发布!"
        });
        $scope.category.content = "";
      }, function (response) {
        $ionicPopup.alert({
          title: "结果",
          template: "发布失败!"
        });
      });
    }
  }])
  .controller("newsBlackBoardCtrl", ["$scope", "$state", "$ionicPopup", "localStorageService", "newsBlackBoardService", function ($scope, $state, $ionicPopup, localStorageService, newsBlackBoardService) {

    $scope.blackBoards = [];
    $scope.getNews = function () {
      newsBlackBoardService.getValidateNews().then(function (response) {
        $scope.blackBoards = response;
      });
    }
    $scope.getNews();
    $scope.data = {};
    $scope.data.staffid = localStorageService.get("staffId");
    $scope.data.newsDetail = function (data) {
      $state.go("newsBlackBoardDetail", {news: data})
    }
    $scope.data.delete = function (data) {
      newsBlackBoardService.delete({newsId: data.id, staffId: $scope.data.staffid}).then(function (response) {
        $scope.getNews();
        $ionicPopup.alert({
          title: "结果",
          template: "删除成功!"
        })
      },function(response){
        $ionicPopup.alert({
          title: "失败",
          template: response.message
        })
      });
    };
  }])
  .controller("newsBlackBoardAddCtrl", ["$scope", "$ionicPopup", "localStorageService", "newsBlackBoardService", function ($scope, $ionicPopup, localStorageService, newsBlackBoardService) {

    $scope.news = {};
    $scope.news.topic = "";
    $scope.values = [{id: 1, name: "一般"}, {id: 2, name: "重要"}, {id: 3, name: "紧急"}]
    $scope.selectedValue = $scope.values[0];
    $scope.submit = function () {
      var data = {};
      data.staffid = localStorageService.get("staffId");
      data.value = $scope.selectedValue.id;
      data.topic = $scope.news.topic;
      data.content = $scope.news.content;
      var date = new Date($scope.news.expireDate);
      data.expiredate = date.getTime();
      newsBlackBoardService.insert(data).then(function (response) {
        $ionicPopup.alert({
          title: "结果",
          template: "成功发布!"
        });
        $scope.news.content = "";
        $scope.news.topic = "";
      }, function (response) {
        $ionicPopup.alert({
          title: "结果",
          template: "发布失败!"
        });
      });
    }
  }])
  .controller("newsBlackBoardDetailCtrl", ["$scope","$sce", "$state", '$stateParams', "$ionicPopup", "localStorageService", "newsBlackBoardService", function ($scope,$sce, $state, $stateParams, $ionicPopup, localStorageService, newsBlackBoardService) {
    $scope.news = $stateParams.news;
    $scope.news.content=$sce.trustAsHtml($stateParams.news.content);
  }])
  .controller("changeTicketCtrl", ["$scope", "$ionicPopup", "$ionicModal", "localStorageService", "ticketOrderService", "busesService", "busOrderService", function ($scope, $ionicPopup, $ionicModal, localStorageService, ticketOrderService, busesService, busOrderService) {
    var staffId = localStorageService.get("staffId");
    $scope.noValidTickets = false;
    $scope.newSeatNumber;
    $scope.selectedSeatNumberChange = function (seatNumber) {
      $scope.newSeatNumber = seatNumber;
    }
    $scope.getValidateTickets = function () {
      ticketOrderService.getValidateTicketsInfoByStaffId({staffId: staffId}).then(function (response) {
        $scope.validateTickets = response;
      }, function (response) {
        $scope.noValidTickets = true;
      }).finally(function () {
      });
    };
    $scope.getValidateTickets();
    $scope.changeTicketInfo = function () {
      var data = {};
      data.staffId = staffId;
      data.ticketId = $scope.ticketOrigin.id;
      data.newBusId = $scope.busNew.id;
      data.newSeatNumber = $scope.newSeatNumber;
      ticketOrderService.changeTicketInfo(data).then(function (response) {
        $ionicPopup.alert({
          title: "成功",
          template: "已成功更换车票!"
        });
        $scope.closeModal();
        $scope.getValidateTickets();
      }, function (response) {
        $ionicPopup.alert({
          title: "失败",
          template: response.message
        });
      })
    }
    $scope.buses = [];
    $scope.getBusesByBusorder = function (busOrderId) {
      busesService.getInfoByBusOrderId({busOrderId: busOrderId}).then(function (response) {
        $scope.buses = response;
      });
    }
    $scope.leftSeats = [];
    $scope.getSeatsNumberLeft = function (busId) {
      ticketOrderService.getSeatsNumberLeft({busId: busId}).then(function (response) {
        $scope.leftSeats = response;
      });
    }

    $scope.selectedBusChange = function (bus) {
      $scope.busNew = bus;
      $scope.getSeatsNumberLeft(bus.id);
    }
    $scope.ticketOrigin = {};
    $scope.ticketNew = {};
    $ionicModal.fromTemplateUrl('ticketChangeModal', {
      scope: $scope,
      animation: 'slide-in-up'
    }).then(function (modal) {
      $scope.modal = modal;
    });
    $scope.openModal = function (ticket) {
      $scope.ticketOrigin = ticket;
      $scope.getBusesByBusorder(ticket.busOrderId);
      $scope.modal.show();
    };
    $scope.closeModal = function () {
      $scope.modal.hide();
    }
  }]);
